﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class ShopPanel : MonoBehaviour
{
    private ManagerVars vars;
    private Transform parent;
    //皮肤名字
    private Text txt_Name;
    private Text txt_Diamond;

    private Button btn_Back;
    //选择
    private Button btn_Select;
    //购买
    private Button btn_Buy;
    //当前选中的下标
    private int currentIndex;

    private void Awake()
    {
        //监听自身
        EventCenter.AddListener(EventDefine.ShowShopPanel, Show);
        parent = transform.Find("ScrollRect/Parent");
        txt_Name = transform.Find("txt_Name").GetComponent<Text>();
        txt_Diamond = transform.Find("Diamond/Text").GetComponent<Text>();

        //按钮监听
        btn_Back = transform.Find("btn_Back").GetComponent<Button>();
         btn_Back.onClick.AddListener(OnBackOnClick);
        btn_Select = transform.Find("btn_Select").GetComponent<Button>();
         btn_Select.onClick.AddListener(OnSelectOnClick);
        btn_Buy = transform.Find("btn_Buy").GetComponent<Button>();
         btn_Buy.onClick.AddListener(OnBuyOnClick);

        vars = ManagerVars.GetManagerVars();//赋值
        gameObject.SetActive(false);
    }
    private void OnDestroy()
    {
        EventCenter.RemoveListener(EventDefine.ShowShopPanel,Show); 

    }
    //TOTO 解决没有初始化数据的问题 
        // private void Start()
    //{
      //  Init();
        //默认隐藏
       
   //}
    private void Show() 
    {
        //Debug
        Init();
        gameObject.SetActive(true);
    }
    List<GameObject> gos = new List<GameObject>();
    private void Init()
    {
        gos.ForEach(go=>Destroy(go));
        gos.Clear();
        //设置宽度  默认为0
        parent.GetComponent<RectTransform>().sizeDelta = new Vector2((vars.skinSpriteList.Count+2) * 160,302);
        for (int i = 0; i < vars.skinSpriteList.Count; i++)
        {
            GameObject go=Instantiate(vars.skinChooseItemPre,parent);
            gos.Add(go);
            //获取当前皮肤有没有解锁
            if (GameManager.Instance.GetSkinUnlocked(i)==false)
            {//未解锁
                go.GetComponentInChildren<Image>().color = Color.gray;
            }
            else
            {
                go.GetComponentInChildren<Image>().color = Color.white;
            }
            go.GetComponentInChildren<Image>().sprite=vars.skinSpriteList[i];//更换图片
            go.transform.localPosition = new Vector3((i+1)*160,0,0);
        }
        //打开页面直接定位到选中的UI
        parent.transform.localPosition = new Vector3(GameManager.Instance.GetSelectedSkin()*-160,0);
    }
    private void Update()
    {
        //有偏差，面板中修改父集的位置
        currentIndex=(int)Mathf.Round(parent.transform.localPosition.x / -160.0f);
        if (Input.GetMouseButtonUp(0))
        {//鼠标抬起
            //dotween插件制作动画
            parent.transform.DOLocalMoveX(currentIndex*-160,0.2f);
            //parent.transform.localPosition = new Vector3(currentIndex*-160,0);
        }
        SetItemSize(currentIndex);
        RefreshUI(currentIndex);//获取当前皮肤名字
    }
    private void SetItemSize(int index)
    {
        for (int i = 0; i < parent.childCount; i++)
        {
            if (index==i)
            {//选中当前皮肤-变大
                parent.GetChild(i).GetChild(0).GetComponent<RectTransform>().sizeDelta = new Vector2(160,160) ;
            }
            else
            {//正常
                parent.GetChild(i).GetChild(0).GetComponent<RectTransform>().sizeDelta = new Vector2(80, 80);
            }
        }
    }
    /// <summary>
    /// 当前皮肤名字
    /// </summary>
    private void RefreshUI(int selectIndex)
    {
        txt_Name.text = vars.skinNameList[selectIndex];
        txt_Diamond.text = GameManager.Instance.GetAllDiamond().ToString();

        //刷新
        if (GameManager.Instance.GetSkinUnlocked(selectIndex) == false)//未解锁
        {
            btn_Select.gameObject.SetActive(false);
            btn_Buy.gameObject.SetActive(true);
            btn_Buy.GetComponentInChildren<Text>().text = vars.skinPrice[selectIndex].ToString();//当前皮肤的价格
        }
        else
        {
            btn_Select.gameObject.SetActive(true);
            btn_Buy.gameObject.SetActive(false);
        }
    }
    /// <summary>
    /// 返回
    /// </summary>
    private void OnBackOnClick() 
    {
        //添加监听  广播
        EventCenter.Broadcast(EventDefine.ShowMainPanel);
        //广播事件码--音效
        EventCenter.Broadcast(EventDefine.PlayClickAudio);
        gameObject.SetActive(false);
    }
    /// <summary>
    /// 选择皮肤按钮点击
    /// </summary>
    private void OnSelectOnClick()
    {
        //广播事件码--选择皮肤
        EventCenter.Broadcast(EventDefine.ChangeSkin,currentIndex);
        GameManager.Instance.SetSelectSkin(currentIndex);
        EventCenter.Broadcast(EventDefine.ShowMainPanel);
        gameObject.SetActive(false);
    }
    /// <summary>
    /// 购买
    /// </summary>
    private void OnBuyOnClick()
    {
        int price = int.Parse(btn_Buy.GetComponentInChildren<Text>().text);
        if (price>GameManager.Instance.GetAllDiamond())
        {
            //广播事件--提示信息
            EventCenter.Broadcast(EventDefine.Hint,"钻石不足");
            Debug.LogError("钻石不足，不能购买");
            return;
        }
        GameManager.Instance.UpdateDiamond(-price);
        //游戏的皮肤修改 从灰色变成白色
        GameManager.Instance.SetSkinUnlocked(currentIndex);//解锁
        parent.GetChild(currentIndex).GetChild(0).GetComponent<Image>().color=Color.white;

        //广播事件码--音效
        EventCenter.Broadcast(EventDefine.PlayClickAudio);
    }
}
